package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.BusinessMapper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.RegionMapper;
import com.heima.mapper.VmTypeMapper;
import com.heima.pojo.Node;
import com.heima.pojo.VendingMachine;
import com.heima.pojo.VmType;
import com.heima.pojo.dto.NodeDto;
import com.heima.pojo.dto.RegionInfoPageDto;
import com.heima.pojo.dto.UserDto;
import com.heima.pojo.vo.*;
import com.heima.service.NodeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class NodeServiceImpl implements NodeService {

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private VmTypeMapper vmTypeMapper;

    @Autowired
    private RegionMapper regionMapper;

    @Autowired
    private BusinessMapper businessMapper;

    /**
     * 查询区域详情
     *
     * @param dto
     * @return
     */
    @Override
    public NodePageVo queryRegionInfo(RegionInfoPageDto dto) {
        dto.setPageSize(50);
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<NodeVo> nodePage = nodeMapper.pageQuery(dto);
        // 查询所有的business
        List<BusinessTypeVo> businessTypeVos = businessMapper.queryAllBusiness();
        // 查询所有的node
        List<Node> nodes = nodeMapper.queryNode();
        // 查询所有的region
        List<RegionVo> regionVos = regionMapper.queryAllRegion();
        // 将所有的node封装进regionVo的nodeList中
        regionVos.forEach(regionVo -> {
            for (Node node : nodes) {
                List<Node> nodeList = regionVo.getNodeList();
                String idInNode = node.getRegionId().toString();
                String idInRegionVo = regionVo.getId().toString();
                if (idInRegionVo.equals(idInNode)) {
                    nodeList.add(node);
                }
            }
        });
        // 将对应的regionVo和businessTypeVo封装进nodePage中
        nodePage.forEach(node -> {
            // 封装regionVo
            for (RegionVo regionVo : regionVos) {
                String idInRegionVo = regionVo.getId().toString();
                String idInNode = node.getRegionId().toString();
                if (idInRegionVo.equals(idInNode)) {
                    node.setRegion(regionVo);
                }
            }
            // 封装businessType
            for (BusinessTypeVo typeVo : businessTypeVos) {
                String idInTypeVo = typeVo.getId().toString();
                String idInNode = node.getBusinessId().toString();
                if (idInNode.equals(idInTypeVo)) {
                    node.setBusinessType(typeVo);
                }
            }
        });
        NodePageVo nodePageVo = new NodePageVo();
        nodePageVo.setPageIndex(dto.getPageIndex());
        nodePageVo.setPageSize(dto.getPageSize());
        nodePageVo.setCurrentPageRecords(nodePage);
        return nodePageVo;
    }

    /**
     * 新增|修改点位
     * @param dto
     */
    @Override
    public void save(NodeDto dto) {
        Node node = new Node();
        BeanUtils.copyProperties(dto, node);
        if (!StringUtils.hasText(dto.getId().toString())){
            node.setCreateUserId(1L);
            node.setCreateTime(LocalDateTime.now());
            node.setUpdateTime(LocalDateTime.now());
            nodeMapper.insert(node);
        }else {
            node.setUpdateTime(LocalDateTime.now());
            nodeMapper.update(node);
        }



    }

    /**
     * 查询点位详情
     *
     * @param nodeId
     * @return
     */
    @Override
    public List<VendingMachineVo> getDetailedNode(String nodeId) {
        // 根据nodeId查询vendingMachineVos
        List<VendingMachineVo> machineVos = nodeMapper.getDetailedNode(Long.parseLong(nodeId));
        List<VendingMachineVo> vo = addData(machineVos, nodeId);
        return vo;
    }

    /**
     * 删除点位
     * @param nodeId
     */
    @Override
    public void deleteById(String nodeId) {
        nodeMapper.deleteById(Long.parseLong(nodeId));
    }

    /**
     * 点位数量
     * @param
     * @return
     */
    @Override
    public Integer count() {
        return nodeMapper.count();
    }

    @Override
    public List<NodeCountVo> nodeCount() {
        return nodeMapper.nodeCount();
    }

    /**
     * 此方法用于在regionVo中添加nodeList
     */
    public RegionVo addData(RegionVo regionVo) {
        List<Node> nodes = nodeMapper.queryNode();
        nodes.forEach(node -> {
            if (regionVo.getId().toString().equals(node.getRegionId().toString())) {
                regionVo.getNodeList().add(node);
            }
        });
        return regionVo;
    }

    /**
     * 此方法用于往machineVos添加nodeVo
     */
    public List<VendingMachineVo> addData(List<VendingMachineVo> machineVos, String nodeId) {
        // 根据nodeId查询nodeVo
        NodeVo nodeVo = nodeMapper.queryById(nodeId);
        // 从nodeVo中取出regionId并查询对应的regionVo
        RegionVo regionVo = regionMapper.queryRegion(nodeVo.getRegionId().toString());
        // 往regionVo中的nodeList中添加数据
        RegionVo doneRegionVo = addData(regionVo);
        // 将doneRegionVo封装进nodeVo
        nodeVo.setRegion(doneRegionVo);
        // 将businessType封装进nodeVo
        List<BusinessTypeVo> businessTypeVos = businessMapper.queryAllBusiness();
        businessTypeVos.forEach(businessTypeVo -> {
            if (businessTypeVo.getId().toString().equals(nodeVo.getBusinessId().toString())) {
                nodeVo.setBusinessType(businessTypeVo);
            }
        });
        //查询所有的vmType
        List<VmType> vmTypes = vmTypeMapper.queryType();
        machineVos.forEach(machineVo -> {
            machineVo.setNode(nodeVo);
            vmTypes.forEach(vmType -> {
                if (vmType.getTypeId().equals(machineVo.getVmType())){
                    machineVo.setType(vmType);
                }
            });
            machineVo.setRegion(nodeVo.getRegion());
        });
        return machineVos;
    }
}
